/*
 * Alloy Analyzer
 * Copyright (c) 2004 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.gui;

import java.util.Iterator;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import alloy.util.Dbg;
import alloy.util.Msg;
import alloy.util.Params;

class GUIMsgListener implements Dbg.DbgMsgListener, ListSelectionListener {

    Vector msgVector;

    boolean errors;

    ModelPanel _textWindow;
    JList _compilerMsgList;
    DefaultListModel _compilerListModel;

    public GUIMsgListener(ModelPanel textWindow_, JList compilerMsgList_) {
	_textWindow = textWindow_;
	_compilerMsgList = compilerMsgList_;
	_compilerListModel = (DefaultListModel)compilerMsgList_.getModel();
	errors = false;

	_compilerMsgList.addListSelectionListener(
						  (ListSelectionListener)AlloySwingUtilities.listener(this));
    }

    public void dbgRegister() {
	Dbg.addDbgMsgListener(Dbg.WARN,   this);
	Dbg.addDbgMsgListener(Dbg.UNIMPL, this);
	Dbg.addDbgMsgListener(Dbg.USER,   this);
    }

    public void dbgDeregister() {
	Dbg.removeDbgMsgListener(Dbg.WARN,   this);
	Dbg.removeDbgMsgListener(Dbg.UNIMPL, this);
	Dbg.removeDbgMsgListener(Dbg.USER,   this);
    }

    public boolean hasErrors() {
	return errors;
    }

    public void clearErrors() {
	errors = false;
	msgVector = new Vector();
	_compilerListModel.clear();
    }

    public void valueChanged(ListSelectionEvent e) {
	int selectedItem;

	selectedItem = _compilerMsgList.getMinSelectionIndex();
	if (selectedItem == -1)
	    return;

	Object[] msgArray = (Object[])msgVector.elementAt(selectedItem);
	_textWindow.fireModelError(msgArray);
    }

    public void show() {
	// Here we convert the Vector into list items.
	Iterator msgIter;
	Object[] msgArray = new Object[2];
	StringBuffer msgString;
	int msgType;
	Msg msg;

	msgIter = msgVector.iterator();
	while (msgIter.hasNext()) {
	    msgArray = (Object[])msgIter.next();
	    msgType = ((Integer)msgArray[0]).intValue();
	    msg = (Msg)msgArray[1];

	    msgString = new StringBuffer();
	    //Font textFont = Params.glob.getFontParam("GUI", "textfont");
	    //msgString.append("<html><font size=-1 face=\""+textFont.getFamily()+"\">");

	    // style stuff is broken?
	    //msgString.append(textFont.isBold() ? "<b>" : "");
	    //msgString.append(textFont.isItalic() ? "<i>" : "");
	    //System.out.println(textFont.isBold()+" "+textFont.isItalic());
	    //System.out.println(textFont.getStyle());

	    msgString.append(msg.getMessage());

	    /*
	      if (msg.getNodeLocString() != null)
	      msgString.append("<br>&nbsp; &nbsp;" + msg.getNodeLocString());

	      if (msg.getNodeString() != null)
	      msgString.append("<br>&nbsp; &nbsp;" + msg.getNodeString());
	    */
	    if ((msg.getNodeLocString() != null) &&
		(Params.glob.getBoolParam("GUI","showErrorLocation")))
		msgString.append("\n" + msg.getNodeLocString());

	    /********/
	    // NO LONGER NECESSARY WITH THE NEW MESSAGE FORMAT -- EMINA
	    /**
	       if (msg.getNodeString() != null)
	       msgString.append("\n  " + msg.getNodeString());
	    **/
	    /*********/

	    //msgString.append(textFont.isBold() ? "</b>" : "");
	    //msgString.append(textFont.isItalic() ? "</i>" : "");

	    //msgString.append("</font>");

	    //System.out.println("IN GUI -- MSG STRING: " + msgString);
	    _compilerListModel.addElement(msgString);
	}
	//if (getSelectedFilePanel() != _textWindow)
	//  _textWindow.showWindow();

	//TODO an attempt to set the font of the messages--doesn't work though
	_compilerMsgList.setFont(Params.glob.getFontParam("GUI", "textfont"));
	//System.out.println(Params.glob.getFontParam("GUI", "textfont"));
	//System.out.println(_compilerMsgList.getFont());

	_textWindow.showMsgPane();
	_textWindow.fireModelChanged();
    }

    public void hide() {
	_textWindow.hideMsgPane();
    }

    public void debugMsg(int msgType_, String msg_, Throwable thrown_) {
	debugMsg(msgType_, new Msg(msg_), thrown_);
    }

    public void debugMsg(int msgType_, Msg msg_, Throwable thrown_) {
	Object[] msgArray = new Object[2];

	errors = true;

	msgArray[0] = new Integer(msgType_);
	msgArray[1] = msg_;
	msgVector.addElement(msgArray);
    }
}
